<%@page import="web.Html"%>
<%@page import="model.Person"%>
<%@page import="model.Address"%>
<%@page import="model.type.AddressType"%>
<%@page import="model.type.SubjectType"%>
<%@page import="model.type.SubjectAttributeType"%>
<%@page import="model.SubjectAttribute"%>
<%@page import="model.Contact"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Collections"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Locale"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<% 
	String selString = "selected = \"selected\""; 
	SimpleDateFormat dateFormat = new SimpleDateFormat("MMMMM dd, yyyy", Locale.ENGLISH);	

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<link type="text/css" rel="stylesheet" href="./jsDatePick_ltr.min.css" />
<link type="text/css" rel="stylesheet" href="./css/style.css" />
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="./script.js"></script>
<script type="text/javascript" src="./jsDatePick.jquery.min.1.3.js"></script>
</head>
<body>
	<script type="text/javascript">
		window.onload = function(){	
			new JsDatePick({ useMode:2, target:"birthday",	dateFormat:"%F %d, %Y" });
			$('#add_new_address').hide();
			$('#add_new_contact').hide();
			$('#add_new_attribute').hide();
		};
		
		//UPDATE PERSON
	    $(function() {  
	        $("#edit_person").click(function() {	        	
	        	$.ajax({
	        		  type: 'POST',
	        		  url: './person',
	        		  data: $('#person_edit_form').serialize(),
	        		  success : function(data){
	        			  message(data.message, data.classType);
	        			  setTimeout(function() {location.reload(true);},1000);
	        		  },
	        		  dataType: 'json'
	        	});	        	
	        });    
	    });
		
		//UPDATE ADDRESS
	    $(function() {  
	        $("#update_address").click(function() {	        	
	        	$.ajax({
	        		  type: 'POST',
	        		  url: './address',
	        		  data: $('#address_edit_form').serialize(),
	        		  success : function(data){
	        			  message(data.message, data.classType);
	        		  },
	        		  dataType: 'json'
	        	});	        	
	        });    
	    });		
	    	    
	    function deleteAddress(id){	    	 
			$.ajax({
					type : 'DELETE',
					url : './address',
					data : 'id='+id,
					success : $('#tr' + id).fadeOut(),
					error : function() {
						alert('Error');
					}
			});
		}
	    
	    //INSERT NEW ADDRESS
	    $(function() {  
	        $("#insert_address").click(function() {	        	
	        	$.ajax({
	        		  type: 'POST',
	        		  url: './address',
	        		  data: $('#address_insert_form').serialize(),
	        		  success : location.reload(true)
	        	});	        	
	        });    
	    });   
	    
	  //INSERT NEW CONTACT
	    $(function() {  
	        $("#insert_contact").click(function() {	        	
	        	$.ajax({
	        		  type: 'POST',
	        		  url: './contact',
	        		  data: $('#contact_insert_form').serialize(),
	        		  success : function(data){	        			  
	        			  location.reload(true);
	        		  }
	        	});	        	
	        });    
	    });   
	  
	  //INSERT NEW ATTRIBUTE
	    $(function() {  
	        $("#insert_attribute").click(function() {	        	
	        	$.ajax({
	        		  type: 'POST',
	        		  url: './attribute',
	        		  data: $('#attribute_insert_form').serialize(),
	        		  success : function(data){
	        			  message(data.message, data.classType);
	        			  location.reload(true);
	        		  }
	        	});	        	
	        });    
	    });   
		
	  //MAKE CUSTOMER
	    function makeCustomer(id){	    	 
			$.ajax({
					type : 'POST',
					url : './customer',
					data : 'id='+id+'&type='+<% out.print(SubjectType.PERSON); %>,
					success : function(data){
						message(data.message, data.classType);
						$('#make_customer').fadeOut();
						setTimeout(function() {location.reload(true);},1000);
					},
					dataType: 'json'
			});
		}
	  
	  	//DELETE CONTACT
	    function deleteContact(id){	    	 
			$.ajax({
					type : 'POST',
					url : './contact?delete=true',
					data : 'id='+id,
					success : function(data){
						message(data.message, data.classType);
						$('#con' + id).fadeOut();
					},
					dataType: 'json'
			});
		}
	  	
	  	//DELETE ATTRIBUTE
	    function deleteAttribute(id){	    	 
			$.ajax({
					type : 'POST',
					url : './attribute?delete=true',
					data : 'id='+id,
					success : function(data){
						message(data.message, data.classType);
						$('#atr' + id).fadeOut();
					},
					dataType: 'json'
			});
		}
	  
	    function message(string, classType){
	    	$("#message").hide();
	    	$("#message").fadeIn();
			$("#message").text(string);
			$("#message").removeClass();
			$("#message").addClass(classType);
			
		}
	    
		function show(id, hide) {
			$('#' + id).fadeIn();
			$('#' + hide).hide();
		}
	</script>
	<a href="./person">Persons</a>
	<p class="error" id="message"></p>
	<% Person p = (Person)request.getAttribute("person"); %>	
	<h1>
		<% out.println(p); %>
	</h1>
	<form name="person_edit_form" id="person_edit_form" action="#">
	<input type ="hidden" name ="id" id = "id" value=<% out.println(p.getId()); %>/>
	<table>		
		<tr><td>ID: <td><td><% out.println(p.getId()); %></td></tr>
		<tr><td>First Name: <td><td><input type="text" id = "first_name" name = "first_name" value = "<% out.print(p.getFirstName()); %>"></input></td></tr>
		<tr><td>Last Name: <td><td><input type="text" id = "last_name" name = "last_name" value = "<% out.print(p.getLastName()); %>"></input></td></tr>
		<tr><td>Identity: <td><td><input type="text" id = "identity" name = "identity" value = "<% out.print(p.getIdentity()); %>"></input></td></tr>
		<tr><td>Birthday: <td><td><input type="text" id = "birthday" name="birthday" value ="<% out.print(Html.date(p.getBirthday())); %>"></input></td></tr>		
		<tr><td>Created: <td><td><% out.print(Html.date(p.getCreated())); %></td></tr>
		<tr><td>Updated: <td><td><% out.print(Html.date(p.getUpdated())); %></td></tr>		
		<tr><td>Created by: <td>
			<td id="creator"><% out.print(Html.personLink(p.getCreaterEmployee())); %></td>
		</tr>		
		<tr><td>Updated by: <td>
			<td id="updater"> <% out.print(Html.personLink(p.getUpdaterEmployee())); %>	</td>
		</tr>			
		<tr><td>is Customer: <td><td><% out.print(p.isCustomer() ? "Yes" : "No"); %></td></tr>		
		<tr>
			<td><input type="button" id = "edit_person" value="Save" /></td><td></td>
		</tr>		
	</table>
	<% if(!p.isCustomer()) out.print("<input type=\"button\" value=\"Make customer\" id=\"make_customer\" onclick=\"makeCustomer('"+p.getId()+"')\"></input>"); %>
	</form>
	<h2>Address</h2>
	<form name="address_edit_form" id="address_edit_form" action="#">
		<table>
			<% 
			ArrayList<Address> alist = p.getAddressList();
			Collections.sort(alist);
			if(alist.size() == 0)out.println("<tr><td>Person address is unknown</td></tr>");
			else out.println("<tr><th>Type</th><th>Country</th><th>County</th><th>Town</th><th>Street</th><th>Zip code</th></tr>");
			for(Address a: alist){			
				out.print("<tr id =\"tr"+a.getId()+"\"><td>"+
					"<select name =\""+a.getId()+"_addresstype\" >"
						+"<option "+(a.getAddressType()==AddressType.ALTERNATE_ADDRESS?selString:"")+" value = "+AddressType.ALTERNATE_ADDRESS+">"
							+AddressType.getAddressType(AddressType.ALTERNATE_ADDRESS)+"</option>"
						+"<option "+(a.getAddressType()==AddressType.MAIN_ADDRESS?selString:"")+" value = "+AddressType.MAIN_ADDRESS+">"
							+AddressType.getAddressType(AddressType.MAIN_ADDRESS)+"</option>"+					
					"</select></td>"+
					"<td><input type=\"text\" onfocus=\"$('#"+a.getId()+"_country').removeClass('error');\" onblur=\"validate('address_edit_form', '"+a.getId()+"_country')\" name=\""+a.getId()+"_country\" id=\""+a.getId()+"_country\" value = \""+a.getCountry()+"\"></input></td>"+
					"<td><input type=\"text\" onfocus=\"$('#"+a.getId()+"_county').removeClass('error');\" onblur=\"validate('address_edit_form', '"+a.getId()+"_county')\" name=\""+a.getId()+"_county\" id=\""+a.getId()+"_county\" value = \""+a.getCounty()+"\"></input></td>"+
					"<td><input type=\"text\" onfocus=\"$('#"+a.getId()+"_town').removeClass('error');\" onblur=\"validate('address_edit_form', '"+a.getId()+"_town')\" name=\""+a.getId()+"_town\" id=\""+a.getId()+"_town\" value = \""+a.getTownVillage()+"\"></input></td>"+
					"<td><input type=\"text\" onfocus=\"$('#"+a.getId()+"_street').removeClass('error');\" onblur=\"validate('address_edit_form', '"+a.getId()+"_street')\" name=\""+a.getId()+"_street\"  id=\""+a.getId()+"_street\" value = \""+a.getStreetAddress()+"\"></input></td>"+
					"<td><input type=\"text\" onfocus=\"$('#"+a.getId()+"_zipcode').removeClass('error');\" onblur=\"validate('address_edit_form', '"+a.getId()+"_zipcode')\" name=\""+a.getId()+"_zipcode\" id=\""+a.getId()+"_zipcode\" value = \""+a.getZipcode()+"\"></input></td>"+
					"<td><a href =\"#\" class=\"delete\" onclick=\"deleteAddress('"+a.getId()+"')\">delete</a></td>");
				out.println("</tr>");
			}	
			%>
			<% if(alist.size() > 0) 
				out.print("<tr><td><input type = \"button\" value=\"Save\" id=\"update_address\" name=\"update_address\"/></td><td></td><td></td><td></td><td></td></tr>");
			%>
		</table>
	</form>
	<div id="add_new_address">
	<% request.setAttribute("address_edit_subject_id", p.getId()); %>
	<% request.setAttribute("address_edit_subject_type", SubjectType.PERSON); %>
	<% request.setAttribute("address_edit_address_type", (alist.size() > 0 ? AddressType.ALTERNATE_ADDRESS : AddressType.MAIN_ADDRESS)); %>
	<%@ include file="address.jsp" %>
	</div>
	<a id = "adna_but" href="#" onclick="show('add_new_address', 'adna_but');">Add new Address</a>
	<h2>
		Contact
	</h2>	
	<table>
		<% 
		ArrayList<Contact> clist = p.getContactList();
		Collections.sort(clist);
		if(clist.size() == 0)out.println("<tr><td>Enterprise has no contacts</td></tr>");
		else out.println("<tr><th>Type</th><th>Value</th><th>Note</th></tr>");
		for(Contact c: clist){			
			out.println("<tr id=\"con"+c.getId()+"\"><td>"+c.getContactTypeString()+"</td><td>"+c.getValueText()+"</td><td>"+c.getNote()+"</td><td><a href=\"#\" class=\"delete\" onclick=\"deleteContact('"+c.getId()+"')\">delete</a></td></tr>");
		}
		%>
	</table>
	<div id="add_new_contact">
	<form name="contact_insert_form" id="contact_insert_form" action ="#">
		<input type="hidden" name ="contact_subject" value = <% out.print(p.getId()); %>></input>
		<input type="hidden" name ="contact_subjecttype" value = <% out.print(SubjectType.PERSON); %>></input>
		<table>
		<tr><th>Contact type</th><th>Value</th><th>Note</th><th>Order</th></tr>
		<tr>
			<td>
				<select name = "contact_type">
					<option value = 1>E-mail</option>
					<option value = 2>Phone</option>
				</select>
			</td>
			<td><input type="text" name ="contact_text"></input></td>	
			<td><input type="text" name ="contact_note"></input></td>				
			<td>
				<select name = "contact_order">
					<option value = 1>High priority</option>
					<option value = 2>Medium priority</option>
					<option value = 3>Low priority</option>
				</select>
			</td>		
		</tr>
		<tr><td><input type="button" id="insert_contact" value="Post"></input></td><td></td><td></td><td></td><td></td></tr>
		</table>
	</form>	
	</div>
	<a id = "adnc_but" href="#" onclick="show('add_new_contact', 'adnc_but');">Add new contact</a>
	<h2>
		Person attributes:
	</h2>	
	<table>
		<% 
		ArrayList<SubjectAttribute> attrlist = p.getAttributes();
		Collections.sort(attrlist);
		if(attrlist.size() == 0)out.println("<tr><td>Person has no attributes</td></tr>");
		else out.println("<tr><th>Type</th><th>Value</th></tr>");
		for(SubjectAttribute a: attrlist){			
			out.println("<tr id=\"atr"+a.getId()+"\"><td>"+a.getSubjectAttributeTypeObject().getName()+"</td><td>"+a.getValue()+"</td><td>"
				+(!a.getSubjectAttributeTypeObject().isRequired() ? "<a href=\"#\" class=\"delete\" onclick=\"deleteAttribute('"+a.getId()+"')\">delete</a>":"")+"</td></tr>");
		}
		%>		
	</table>
	<div id="add_new_attribute">
	<form name="attribute_insert_form" id="attribute_insert_form" action ="#">
		<input type="hidden" name ="attribute_subject" value = <% out.print(p.getId()); %>></input>
		<input type="hidden" name ="attribute_subject_type" value = <% out.print(SubjectType.PERSON); %>></input>
		<table>
		<tr><th>Attribute type</th><th>Value</th></tr>
		<tr>
			<td>
				<select name = "attribute_type">
					<% 
						for(SubjectAttributeType s: p.getAvailableAttributes()){
							out.println("<option value = "+s.getId()+">"+s.getName()+"</option>");
						}					
					%>									
				</select>
			</td>
			<td><input type="text" name ="attribute_value"></input></td>
			<td>
				<select name = "attribute_order">
					<option value = 1>High priority</option>
					<option value = 2>Medium priority</option>
					<option value = 3>Low priority</option>
				</select>
			</td>							
		</tr>
		<tr><td><input type="button" id="insert_attribute" value="Post"></input></td><td></td><td></td></tr>
		</table>
	</form>	
	</div>
	<a id ="adnatr_but" href="#" onclick="show('add_new_attribute', 'adnatr_but');">Add new attribute</a>	
	
</body>
</html>